﻿using System;

using System.Runtime.Intrinsics.X86;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Interop;
using Caliburn.Micro;
using HandyControl.Controls;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
using ThunderboltCustomControlLibrary;
using ThunderboltHelper;
using ThunderboltInterfaceSharp.Views.UserControls;
using Action = System.Action;

namespace ThunderboltInterfaceSharp.ViewModels.UserControls;

public class TopFunctionBarViewModel:Screen
{
	public delegate void WindowChangeCommand(WindowChange command);
	public event WindowChangeCommand WindowChangeEvent ;
	public Action ShowLoginWindowEvent;
	public WindowChange CurrentCommand = WindowChange.Moderate;
	public string X { get; set; }
	public string Y { get; set; }
	public bool IsShowSearchBorder { get; set; }
	public bool IsShowLoginGuide { get; set; }
	public bool IsMouseInLoginGuide { get; set; }
	public double VOffset { get; set; }
	public double HOffset { get; set; }


	private TopFunctionBarView _topFunctionBar;
	public TopFunctionBarViewModel()
	{
	}

	public void BtnMinClick()
	{
		CurrentCommand = WindowChange.Minimize;
		OnWindowChange();
	}
	public void BtnMaxClick()
	{
		CurrentCommand = WindowChange.Maximize;
		OnWindowChange();
	}
	public void BtnCloseClick()
	{
		CurrentCommand = WindowChange.Close;
		OnWindowChange();
	}

	public void OnWindowChange()
	{
		if (WindowChangeEvent != null)
		{
			WindowChangeEvent(CurrentCommand);
		}
	}
	public void OnShowLoginWindow()
	{
		if (ShowLoginWindowEvent != null)
		{
			ShowLoginWindowEvent();
		}
	}

	public void BtnLoginClick()
	{
		OnShowLoginWindow();
	}



	public void HeadPortraitMouseEnter()
	{
		IsShowLoginGuide = true;
	}
	public void HeadPortraitMouseLeave(MouseEventArgs e)
	{
		Task.Run(() =>
		{
			Thread.Sleep(500);
			if(IsMouseInLoginGuide) return;
			//var location = e.GetPosition(_topFunctionBar.LoginGuidePopup);
			IsShowLoginGuide = false;
		});

	}
	public void LoginGuidePopupMouseEnter()
	{
		IsMouseInLoginGuide = true;
	}
	public void LoginGuidePopupMouseLeave(MouseEventArgs e)
	{

		IsMouseInLoginGuide = false;
		Thread.Sleep(500);
		IsShowLoginGuide = false;
	}



	protected override void OnViewLoaded(object view)
	{
		base.OnViewLoaded(view);
		_topFunctionBar = view as TopFunctionBarView;
	}


	//source 和 this
	//source传递SearchTextBox，this传递文本
	public void SearchTextBoxLeftButtonDown(object sender,MouseButtonEventArgs e)
	{
		IsShowSearchBorder = true;
		//定位popup的位置，使之位于当前单元格的下边缘
		var txtSearch = sender as PlaceholderTextBox;
		var position = txtSearch.TranslatePoint(new Point(), _topFunctionBar); //获取当前单元格的位置,dgVoucher为DataGrid名称
		//580是Popup的宽度
		HOffset = position.X - (580-txtSearch.ActualWidth)*0.5; //设置横坐标偏移量 
		VOffset = position.Y -2; //纵坐标偏移量往上一丢丢

		//必须显式让 Popup 获得焦点，否则内部的 TextBox 输入时，输入框无法跟随。
		var source = (HwndSource)PresentationSource.FromVisual(_topFunctionBar.SearchTextBoxPopup.Child);
		UniversalMethod.SetFocus(source.Handle);
		//必须显式让文本框获得焦点（如果有的话）。
		Keyboard.Focus(_topFunctionBar.SearchTextBoxInPopup);

	}

	public void SearchTextBoxPopupLoaded()
	{
		//跟随窗体一起Loaded,并不是弹出来的时候调用的
	}


}

public enum WindowChange : byte
{
	Minimize=0,
	Maximize,
	Moderate,
	Close
}